﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "ApplicationManager.h"
#include "IApplication.h"
#include "IApplicationConfig.h"
#include "IMainWindow.h"
#include "IAddinManager.h"
#include "IAuthenticationManager.h"
#include <QtCore/QCoreApplication>
#include <qmessagebox.h>
#include "ApplicationImpl.h"
#include "IMeshDiscretizeOptions.h"

using namespace gcmp;

int main(int argc, char** argv)
{
    //设置主界面可自由拖动
    QCoreApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);

    //1、创建主应用程序，有且只有一个
    IApplication* pApplication = ApplicationManager::Create(argc, argv);

    Sample::ApplicationObserver observer{ pApplication->Get() };
    observer.Init(pApplication->Get());

    //1.3、设置插件目录
    if (auto pAddinManager = pApplication->GetAddinManager())
    {
        pAddinManager->SetAddinConfigDirectory(L"Application_plugin_config");
    }

    //2、创建主窗口，有且只有一个，main函数维护生命周期
    OwnerPtr<IApplicationWindow> opMainWindow = IMainWindow::Create();

    // MainWindwo创建后，需要初始化二维组件内部控件的皮肤
    Sample::InitUiSkin(opMainWindow.get());

    observer.BindWindowEvent(opMainWindow.get());

    // 设置几何图形在离散化时的精度
    gcmp::IMeshDiscretizeOptions::Get()->SetMeshDiscretizePrecision(10, 0.0872664625997165);

    //3、执行应用程序
    int result = pApplication->Execute();
    if (result != 0)
    {
        if (gcmp::AUTH_ERROR_CODE == result)
        {
            std::wstring errorMessage = L"授权失败";
            gcmp::IAuthenticationManager* pAuthManager = gcmp::IAuthenticationManager::Get();
            if (pAuthManager) {
                if (AUTHErrCode::ERROR_FIND_PRODUCT == pAuthManager->GetErrorCode()) {
                    errorMessage = L"运行前请先激活授权";
                }
                else {
                    errorMessage = pAuthManager->GetErrorMessage(pAuthManager->GetErrorCode());
                }
            }
            QMessageBox::critical(NULL, QString::fromStdWString(L"授权错误"), QString::fromStdWString(errorMessage));
            ExitProcess(0);
        }
        DBG_WARN_UNLESS(0, L"执行中发生错误", L"GDMP", L"2023-09-30");
        ExitProcess(0);
    }

    //4、重置主窗口
    opMainWindow.reset();

    //5、退出前销毁主程序
    ApplicationManager::Destroy();
    return result;
}
